import 'package:backend.core/core/domain/entities/app_store/app_store_purchased_subscription_expiration_reason.dart';
import 'package:backend.core/core/domain/entities/app_store/app_store_purchased_subscription_info.dart';
import 'package:backend.purchases/domain/entities/app_store_api/app_store_api_environment.dart';
import 'package:backend.purchases/domain/entities/app_store_api/app_store_api_purchased_subscription_renewal_info.dart';
import 'package:backend.purchases/domain/helpers/app_store/create_app_store_expiration_reason_from_api_one.dart';
import 'package:backend.purchases/domain/helpers/app_store/create_app_store_price_increase_status_from_api_one.dart';

abstract class CreateAppStorePurchasedSubscriptionInfoFromApiOne {
  AppStorePurchasedSubscriptionInfo call({
    required AppStoreApiPurchasedSubscriptionRenewalInfo apiRenewalInfo,
    required String appStoreUserId,
  });
}

class CreateAppStorePurchasedSubscriptionInfoFromApiOneImpl
    implements CreateAppStorePurchasedSubscriptionInfoFromApiOne {
  const CreateAppStorePurchasedSubscriptionInfoFromApiOneImpl({
    required CreateAppStorePurchasedSubscriptionExpirationReasonFromApiOne
        createAppStorePurchasedSubscriptionExpirationReasonFromApiOne,
    required CreateAppStorePurchasedSubscriptionPriceIncreaseStatusFromApiOne
        createAppStorePurchasedSubscriptionPriceIncreaseStatusFromApiOne,
  })  : _createAppStorePurchasedSubscriptionExpirationReasonFromApiOne =
            createAppStorePurchasedSubscriptionExpirationReasonFromApiOne,
        _createAppStorePurchasedSubscriptionPriceIncreaseStatusFromApiOne =
            createAppStorePurchasedSubscriptionPriceIncreaseStatusFromApiOne;

  final CreateAppStorePurchasedSubscriptionExpirationReasonFromApiOne
      _createAppStorePurchasedSubscriptionExpirationReasonFromApiOne;
  final CreateAppStorePurchasedSubscriptionPriceIncreaseStatusFromApiOne
      _createAppStorePurchasedSubscriptionPriceIncreaseStatusFromApiOne;

  @override
  AppStorePurchasedSubscriptionInfo call({
    required AppStoreApiPurchasedSubscriptionRenewalInfo apiRenewalInfo,
    required String appStoreUserId,
  }) {
    AppStorePurchasedSubscriptionExpirationReason? expirationReason;

    if (apiRenewalInfo.expirationReason != null) {
      expirationReason = _createAppStorePurchasedSubscriptionExpirationReasonFromApiOne(
        apiExpirationReason: apiRenewalInfo.expirationReason!,
      );
    }

    final priceIncreaseStatus = _createAppStorePurchasedSubscriptionPriceIncreaseStatusFromApiOne(
      apiPriceIncreaseStatus: apiRenewalInfo.priceIncreaseStatus,
    );

    return AppStorePurchasedSubscriptionInfo(
      appStoreUserId: appStoreUserId,
      purchaseItemSku: apiRenewalInfo.purchaseItemSku,
      originalTransactionId: apiRenewalInfo.originalTransactionId,
      autoRenewalEnabled: apiRenewalInfo.autoRenewalEnabled,
      isInBillingRetryPeriod: apiRenewalInfo.isInBillingRetryPeriod,
      priceIncreaseStatus: priceIncreaseStatus,
      test: apiRenewalInfo.environment == AppStoreApiEnvironment.sandbox,
      expirationReason: expirationReason,
      gracePeriodExpirationTime: apiRenewalInfo.gracePeriodExpirationTime,
    );
  }
}
